<template>
  <!-- 合同新增 -->
  <el-dialog v-dialogDrag :title="dialogTitle" :close-on-click-modal="false" class="dialogStyleMiddle" :visible.sync="dialogVisible">
    <el-form ref="form" :model="formData" label-width="100px" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同名称" prop="contractName">
            <el-input v-model="formData.contractName" clearable :readonly="isReadonly" placeholder="请输入合同名称" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建设单位" prop="construction">
            <el-input v-model="formData.construction" clearable :readonly="isReadonly" placeholder="请输入建设单位" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同工期" prop="contractDuration">
            <el-input v-model="formData.contractDuration" clearable :readonly="isReadonly" placeholder="请输入合同工期" size="medium">
              <template slot="append">天</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同单价" prop="contractPriceName">
            <el-input v-model="formData.contractPriceName" clearable :readonly="isReadonly" placeholder="请输入合同单价" size="medium" @change="contractPrice($event)">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同总金额" prop="contractAmountName">
            <el-input v-model="formData.contractAmountName" clearable :readonly="isReadonly" placeholder="请输入合同总金额" size="medium" @change="contractAmount($event)">
              <template slot="append">万元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="钻孔总数" prop="drillingCount">
            <el-input v-model="formData.drillingCount" clearable :readonly="isReadonly" placeholder="请输入钻孔总个数" size="medium">
              <template slot="append">个</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="钻孔总米数" prop="drillMember">
            <el-input v-model="formData.drillMember" clearable :readonly="isReadonly" placeholder="请输入钻孔总米数" size="medium">
              <template slot="append">m</template>
            </el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最小深度" prop="minScope">
            <el-input v-model="formData.minScope" clearable :readonly="isReadonly" size="medium" placeholder="最小勘探深度">
              <template slot="append"><el-button />m</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最大深度" prop="maxScope">
            <el-input v-model="formData.maxScope" clearable :readonly="isReadonly" size="medium" placeholder="最大勘探深度">
              <template slot="append"><el-button />m</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="合同附件" prop="appFiles">
            <FileUplode
              ref="fileUplode"
              :edit-file-list="conractFileList"
              :read-only="isReadonly"
              @getFileList="getContractFile"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span v-if="!isReadonly" slot="footer" class="dialog-footer">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button v-hasPermi="['supervise:proContract:add']" size="medium" :disabled="btnDisabled" type="primary" @click="submitForm">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addContract, getContractById } from '@/api/projectManage/project/proContract'
// 上传文件
import FileUplode from '@/components/Upload/FileUplode'
// 字段校验
import { integer, validateDouble } from '@/utils/validate'
export default {
  name: 'DetailDialog',
  components: { FileUplode },
  data() {
    return {
      isReadonly: false,
      conractFileList: [], // 回显合同
      /* 新增 start */
      btnDisabled: false,
      dialogTitle: '', // 对话框是否弹出显示
      dialogVisible: false, // 对话框是否弹出显示
      formData: {
        id: '',
        proId: '', // 项目id
        contractName: '', // 合同名称
        construction: '', // 建设单位
        contractPriceName: '',
        contractPrice: '', // 合同单价
        contractDuration: '', // 合同工期
        contractAmountName: '',
        contractAmount: '', // 合同总金额
        drillingCount: '', // 钻孔总个数
        drillMember: '', // 钻孔总米数
        minScope: '', //  勘探最小深度
        maxScope: '', // 勘探最大深度
        appFiles: '', // 合同附件
        fileDelete: '',
        contractType: '0'
      },
      // from表单校验
      rules: {
        contractName: [{ required: true, message: '合同名称不能为空', trigger: 'blur' }],
        construction: [{ required: true, message: '建设单位不能为空', trigger: 'blur' }],
        contractPriceName: [
          { required: true, message: '合同单价不能为空', trigger: 'blur' }
          // { validator: validateDouble, trigger: 'blur' }
        ],
        contractDuration: [
          { required: true, message: '合同工期不能为空', trigger: 'blur' },
          { validator: integer, trigger: 'blur' }
        ],
        contractType: [{ required: true, message: '合同类型不能为空', trigger: 'blur' }],
        contractAmountName: [
          { required: true, message: '合同总金额不能为空', trigger: 'blur' }
          // { validator: moneyValid, trigger: 'blur' }
        ],
        drillingCount: [
          { required: true, message: '钻孔总个数不能为空', trigger: 'blur' },
          { validator: integer, trigger: 'blur' }
        ],
        drillMember: [
          { required: true, message: '钻孔总米数不能为空', trigger: 'blur' },
          { validator: validateDouble, trigger: 'blur' }
        ],
        minScope: [
          { required: true, message: '勘探最小深度不能为空', trigger: 'blur' },
          { validator: validateDouble, trigger: 'blur' }
        ],
        maxScope: [
          { required: true, message: '勘探最大深度不能为空', trigger: 'blur' },
          { validator: validateDouble, trigger: 'blur' }
        ],
        appFiles: [{ required: true, message: '合同附件不能为空', trigger: 'blur' }]
      }
      /* 新增 end */
    }
  },
  methods: {
    // 合同总金额
    contractAmount(e) {
      if (e) {
        this.formData.contractAmountName = this.divideMoney(e, 6)
        this.formData.contractAmount = e * 10000
      }
    },
    // 合同单价
    contractPrice(e) {
      if (e) {
        this.formData.contractPriceName = this.divideMoney(e, 2)
        this.formData.contractPrice = e
      }
    },
    // 获取合同附件
    getContractFile(fileList, deleteFileIds) {
      this.formData.appFiles = fileList
      this.formData.fileDelete = deleteFileIds
    },
    // 初始化弹框
    handleAdd(title, id) {
      this.dialogVisible = true
      this.dialogTitle = title
      // 重置表单
      this.emptyForm()
      this.formData.proId = id
      if (id) {
        this.handleDetail(id)
      }
    },
    handleDetail(id) {
      getContractById(id).then(res => {
        if (res.data) {
          this.dialogTitle = '合同详情'
          this.isReadonly = true
          this.formData = { ...res.data }
          this.formData.contractAmountName = this.divideMoney(res.data.contractAmount, 6, true)
          this.formData.contractPriceName = this.divideMoney(res.data.contractPrice, 2)
          if (res.data.appFiles && res.data.appFiles.length > 0) {
            this.conractFileList = res.data.appFiles
          }
        }
      })
    },
    // 弹框取消按钮
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
    },
    // 保存
    submitForm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.btnDisabled = true
          // 新增
          await addContract(this.formData)
          this.$parent.getList()
          this.msgSuccess('保存成功')
          this.cancel()
        }
      })
    },
    // 清空弹出
    emptyForm() {
      this.resetForm('form')
      this.formData.id = ''
      this.conractFileList = []
      this.isReadonly = false
    }
  }
}
</script>

